package com.vastcom.demo.service.impl;

import com.vastcom.demo.dao.CarouselDao;
import com.vastcom.demo.dao.SinglepageDao;
import com.vastcom.demo.model.Carousel;
import com.vastcom.demo.model.Singlepage;
import com.vastcom.demo.service.ManageService;
import com.vastcom.demo.tools.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

@Service
public class ManageServiceImpl implements ManageService{
    @Autowired
    private SinglepageDao singlepageDao;

    @Autowired
    private CarouselDao carouselDao;


    @Override
    public Carousel saveCarousel(Carousel carousel) {
        try {
            return carouselDao.save(carousel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean deleteCarousel(String id) {
        try {
            carouselDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Carousel getCarousel(String id) {
        Optional<Carousel>temp=carouselDao.findById(id);
        return temp.isPresent()?temp.get():null;
    }

    @Override
    public List<Carousel> getCarouselList() {
        return carouselDao.findAll();
    }


    //接口实现
    //获取当前登录用的的Id
    @Override
    public String getCurrentUserId() {
        String userid= (String) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();
        if(Tools.isNullOrSpace(userid)){
            return null;
        }
        else {
            return userid;
        }
    }
    //获取当前登录用户的角色
    @Override
    public String getCurrentRole() {
        String role=null;
        Collection<SimpleGrantedAuthority> authorities = (Collection<SimpleGrantedAuthority>)    SecurityContextHolder.getContext().getAuthentication().getAuthorities();
        for (GrantedAuthority authority : authorities) {
            role = authority.getAuthority();

        }

        if(Tools.isNullOrSpace(role)){
            return null;
        }
        else{
            return role;
        }
    }




    @Override
    public Singlepage saveSinglepage(Singlepage singlepage) {

        try {
            return singlepageDao.save(singlepage);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean deleteSinglepage(String id) {
        try {
            singlepageDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Singlepage getSinglepage(String id) {
        Optional<Singlepage> temp=singlepageDao.findById(id);
        return temp.isPresent()?temp.get():null;
    }

    @Override
    public List<Singlepage> getSinglepageList() {
        return singlepageDao.findAll();
    }
}
